package com.ternnetwork.core.notice.controller;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.notice.dto.NoticeCategoryDto;
import com.ternnetwork.core.notice.service.NoticeCategoryService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;


@Controller
@RequestMapping("/notice/category/*")
public class NoticeCategoryController {


    private final NoticeCategoryService noticeCategoryService;

    public NoticeCategoryController(NoticeCategoryService noticeCategoryService) {
        this.noticeCategoryService = noticeCategoryService;
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_CATEGORY_ADD')")
    @RequestMapping("/add")
    public @ResponseBody BaseResponse add(String requestJson, Principal user) throws Exception {
        NoticeCategoryDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoticeCategoryDto.class);
        return noticeCategoryService.add(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_CATEGORY_UPDATE')")
    @RequestMapping("/update")
    public @ResponseBody BaseResponse update(String requestJson, Principal user) throws Exception {
        NoticeCategoryDto dto= JsonUtils.jsonToObjectThrowsException(requestJson, NoticeCategoryDto.class);
        return noticeCategoryService.update(dto,user.getName());
    }
    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_CATEGORY_FIND_ALL')")
    @RequestMapping("/findAll")
    public @ResponseBody BaseResponse findAll(Principal user)  {
      return noticeCategoryService.findAll();
    }

    @PreAuthorize("hasAnyAuthority('API_CORE_NOTICE_CATEGORY_DELETE_BY_ID')")
    @RequestMapping("/deleteById")
    public @ResponseBody BaseResponse deleteById(Long id, Principal user) {
        return noticeCategoryService.deleteById(id,user.getName());
    }



}
